package javabase.security;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * <pre>
 *   Title: SecretUtils.java
 *   Description: 加密解密工具类
 *   Copyright: yundaex.com Copyright (c) 2013
 *   Company: 上海韵达货运有限公司
 * </pre>
 * 
 * @author liuzhaoqing
 * @version 1.0
 * @date 2014年11月20日
 */
public class SecretUtils {

    //定义加密算法，有DES、DESede(即3DES)、Blowfish
    private static final String ALGORITHM = "DESede";    
    
    
    /**
     * 加密方法
     * @param cryptKEY 加密key
     * @param src 源数据的字节数组
     * @return 
     */
    public static byte[] encryptMode(String cryptKEY, byte[] src) {
        try {
             SecretKey deskey = new SecretKeySpec(build3DesKey(cryptKEY), ALGORITHM);    //生成密钥
             Cipher c1 = Cipher.getInstance(ALGORITHM);    //实例化负责加密/解密的Cipher工具类
             c1.init(Cipher.ENCRYPT_MODE, deskey);    //初始化为加密模式
             return c1.doFinal(src);
         } catch (java.security.NoSuchAlgorithmException e1) {
             e1.printStackTrace();
         } catch (javax.crypto.NoSuchPaddingException e2) {
             e2.printStackTrace();
         } catch (java.lang.Exception e3) {
             e3.printStackTrace();
         }
         return null;
     }
    
    
    /**
     * 解密函数
     * @param cryptKEY 解密key
     * @param src 密文的字节数组
     * @return
     */
    public static byte[] decryptMode(String cryptKEY, byte[] src) {      
        try {
            SecretKey deskey = new SecretKeySpec(build3DesKey(cryptKEY), ALGORITHM);
            Cipher c1 = Cipher.getInstance(ALGORITHM);
            c1.init(Cipher.DECRYPT_MODE, deskey);    //初始化为解密模式
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
     }
    
    
    /*
     * 根据字符串生成密钥字节数组 
     * @param keyStr 密钥字符串
     * @return byte[]
     * @throws UnsupportedEncodingException
     */
    public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException{
        byte[] key = new byte[24];    // 声明一个24位的字节数组，默认里面都是0
        byte[] temp = keyStr.getBytes("UTF-8");    //将字符串转成字节数组
        
        /*
         * 执行数组拷贝
         * System.arraycopy(源数组，从源数组哪里开始拷贝，目标数组，拷贝多少位)
         */
        if(key.length > temp.length){
            //如果temp不够24位，则拷贝temp数组整个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, temp.length);
        }else{
            //如果temp大于24位，则拷贝temp数组24个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, key.length);
        }
        return key;
    } 

	 public static void main(String[] args) throws Exception {
	     String msg = "{orderId=14129956248391234, mailNo=1200578945605, receiverName=66, receiverContact=13052180503, receiverProvince=上海市, receiverCity=上海市, receiverArea=青浦区, receiverAddress=44, senderName=kkkk, senderContact=13056561234, senderProvince=上海市, senderCity=上海市, senderArea=青浦区, seenderAddress=kkk}";
	     Map<String, String> map = new HashMap<String, String>();
	     map.put("jdksjf1", "d发到快速减肥");
	     map.put("jdksjf2", "d发到快速减肥房间看风景");
	     map.put("jdksjf3", "d发到快速减肥京东上口");
	     map.put("jdksjf4", "d发到快速减肥iooi");
	     System.out.println("【加密前】：" + (msg=map.toString()));
	     String PASSWORD_CRYPT_KEY = "2012PinganVitality075522628888ForShenZhenBelter075561869839";
	     PASSWORD_CRYPT_KEY = "jslklewusp9877odfzndkjweuiofd657sdskfljklaf378482";
	     //加密
	     byte[] secretArr = SecretUtils.encryptMode(PASSWORD_CRYPT_KEY, msg.getBytes("utf-8"));    
	     System.out.println("【加密后】：" + new String(secretArr));
	     
	     //解密
	     byte[] myMsgArr = SecretUtils.decryptMode(PASSWORD_CRYPT_KEY, secretArr);  
	     System.out.println("【解密后】：" + new String(myMsgArr));
	}

}
